package com.afd.ozblip.activity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.afd.ozblip.adapter.DishListAdapter;
import com.afd.ozblip.entity.DishEntity;
import com.afd.ozblip.net.HttpUtil;
import com.afd.ozblip.util.AsyncAction;
import com.afd.ozblip.util.AsyncLoadData;
import com.afd.ozblip.util.CollectionUtil;
import com.afd.ozblip.util.Constants;
import com.afd.ozblip.util.OzblipConfig;
import com.afd.ozblip.util.ParseJson;
import com.afd.ozblip.util.StringUtil;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class BusinessDishActivity extends BaseActivity {

	/****************** 视图变量 **************************/
	private ListView dishListView;
	/****************** 其它变量 **************************/
	private List<DishEntity> dishList;
	private DishListAdapter dishListAdapter;
	private String businessId;
	private String businessName;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.business_dish_list);
		// 启动返回功能
		toBack();
		// 寻找视图
		findViews();
		// 设置监听
		setListeners();
		// 获取参数
		getBundle();
		// 获取列表数据
		requestTopListData();

	}

	/**
	 * 寻找视图
	 */
	public void findViews() {
		this.dishListView = (ListView) findViewById(R.id.lv_business_dish);
	}

	/**
	 * 设置监听
	 */
	public void setListeners() {

		View.OnClickListener moreClick = new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.ll_more_layout_setting:
					Intent settingIntent = new Intent(
							BusinessDishActivity.this, SettingActivity.class);
					startActivity(settingIntent);
					break;

				default:
					break;
				}
			}
		};
		this.dishListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> itemView, View arg1,
					int parameInt, long arg3) {
				Intent businessIntent = new Intent(BusinessDishActivity.this,
						DishDetailActivity.class);
				DishEntity dish = BusinessDishActivity.this.dishList
						.get(parameInt);
				dish.setBusinessName(businessName);
				businessIntent.putExtra("dish", dish);
				startActivity(businessIntent);
			}
		});
	}

	/**
	 * 获取传递的参数
	 */
	private void getBundle() {
		Intent intent = getIntent();
		businessId = intent.getStringExtra("businessId");
		businessName = intent.getStringExtra("businessName");
	}

	/**
	 * 获取评论列表数据
	 */
	private void requestTopListData() {
		if (StringUtil.isNullOrEmpty(businessId)) {
			return;
		}
		AsyncLoadData ald = new AsyncLoadData(this, "加载中...",
				new AsyncAction() {

					@Override
					public void doLoad() throws Exception {
						// 获取提交参数
						Map<String, Object> nearbyBusinessParamMap = new HashMap<String, Object>();
						nearbyBusinessParamMap.put("sellerName", businessId);
						// nearbyBusinessParamMap.put("dishName", "菜名");
						nearbyBusinessParamMap.put("pageCnt", "1");
						nearbyBusinessParamMap.put("pageRow",
								OzblipConfig.seller_pageSize);
						nearbyBusinessParamMap.put("sort", "1");
						nearbyBusinessParamMap.put("language", commonLanguage);
						// 发送请求
						byte[] commentItemList = HttpUtil.requestByPost(
								BusinessDishActivity.this,
								Constants.SELLER_DISH_LIST_API,
								nearbyBusinessParamMap);
						if (commentItemList != null) {
							dishList = new ParseJson()
									.parseBusinessDishListJson(commentItemList);
						}
					}

					@Override
					public void loadEnd() throws Exception {
						setDishListAdapter();
					}

				});
		ald.execute(0);
	}

	/**
	 * 调用菜单列表适配器
	 */
	public void setDishListAdapter() {
		if (CollectionUtil.UnNullAndZero(dishList)) {
			this.dishListAdapter = new DishListAdapter(
					BusinessDishActivity.this);
			this.dishListAdapter.addItems(dishList);
			this.dishListView.setAdapter(this.dishListAdapter);
		}
	}
}